<?php

namespace app\api\controller\v2;


use app\api\service\HuaWeiOBS;
use app\common\controller\Api;
use think\Config;
use think\Exception;
use think\Request;
use think\View;


/**
 * 测试控制器
 */
class Test extends Api
{
    //无需登录的方法,同时也就不需要鉴权了
    protected $noNeedLogin = ['*'];
    //无需鉴权的方法,但需要登录
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    public function index()
    {
        $this->aa();
    }

    public function aa()
    {
        $url = 'http://apis.juhe.cn/fapigx/caijing/query';
        try {
            $word = '财经';
            $postData = [
                'key'=>'17eb7722f441032f6cfd825303f6658b',//在个人中心->我的数据,接口名称上方查看
                'num'=>100,//返回数量
                'page'=>1,//翻页
//                'rand'=>1,//是否随机获取
                'word'=>$word,//检索关键词
            ];

            $curl = curl_init();
            curl_setopt_array($curl, [
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => http_build_query($postData),
                CURLOPT_HTTPHEADER => [
                    "Content-Type: application/x-www-form-urlencoded"
                ],
            ]);

            $response = curl_exec($curl);
            curl_close($curl);

            $resData = json_decode($response, true);
           dd($resData);
        }catch (Exception $exception){
            dd($exception->getMessage());
            $res['code']= 500;
            $res['msg']= '系统错误:' . $exception->getMessage() . $exception->getLine();
        }
dd(123);
    }

    //获取分段上传初始化参数
    public function initiateMultipartUpload()
    {
        $file_name = $this->request->param('file_name');
        $file_name = time() . rand(1111, 9999) . '.' . pathinfo($file_name, PATHINFO_EXTENSION);
        $path = 'uploads/' . date('Ymd') . '/' . $file_name;
        $huaWeiOBS = new HuaWeiOBS();
        $data = $huaWeiOBS->initiateMultipartUpload($path);
        $res = [
            'path' => $path,
            'upload_id' => $data['UploadId'],
        ];
        return $this->success('', $res);
    }

    //开始上传分片
    public function uploadPart()
    {
        $file = $_FILES['chunk'];
        $source_file = $file['tmp_name'];//待上传对象的源文件路径
        $upload_id = $this->request->param('upload_id');
        $path = $this->request->param('path');
        $part_number = $this->request->param('part_number');//设置分段号，范围是1~10000

        $huaWeiOBS = new HuaWeiOBS();
        $data = $huaWeiOBS->uploadPart($source_file, $path, $upload_id, $part_number);
        $res = [
            'PartNumber' => $part_number,
            'ETag' => $data['ETag'],
        ];
        return $this->success('', $res);
    }

    //合并上传文件
    public function completeMultipartUpload(Request $request)
    {

        $upload_id = $this->request->param('upload_id');
        $parts = $_POST['parts'];
        $path = $this->request->param('path');

        $huaWeiOBS = new HuaWeiOBS();
        $data = $huaWeiOBS->completeMultipartUpload($upload_id, $parts, $path);
        dd($data);
    }

    public function shehedev(){
        // 审核通过后title为'直播广场'
        return $this->success('success',['title'=>'直播广场']);
    }
}
